09-06 - Variables

Go and work
Honor the One who is working on you.
Do so by making beautiful things,
Be serving in beautiful ways,
By speaking up for the weak whose beauty is being maligned,
By filling this city with the aroma of good and beautiful work

Reflect the beautiful work of your heavenly Father,
Nourished now by the grace and mercy of Christ,
In the power of the Holy Spirit.

God has already accomplished the great work.
God goes before you and behind you.
God works at your side.

(Matthew Kaemingk & Cory B. Willson, Work and Worship, p. 140)

Retrieval

  • What are programs? What are computations? What are algorithms?
  • What is code?
  • What is a programming language?
  • Why it can be hard to use a programming language?

Objects and Variables

Object

  • Python syntax specifies some ways to represent different types of data. A data representation in Python is called an “object”.
Type Object type in Python Example
Integer number int 123
Decimal number (floating point) float 3.14
Logic value bool True, False
Text string "Hello World!"

Variables

  • Variables are names we set to refer to objects.
    • A not-so-good metaphor: variables are containers for objects
    • A better metaphor: objects are houses, variables are addresses of these houses
x = 123  # a variable x that contains the integer value 123
x = x + 1  # x is updated with the value of x + 1, becoming 124...
hello = "Hello World!"  # a variable that contains the string "Hello World!"
is_done = True  # a variable is_done with the logic value True

Objects x variables

  • It is very important to differentiate!
  • Which of the following are variables and which are objects?
"hello"

hello

132

var_1

truev

True

Variable naming conventions in Python

  • They MUST start with a letter or with _ (underline)
  • They are case sensitive (‘C’ is different from ‘c’)
  • They can’t contain: { ( + - * / \ ; . , ?
  • They can’t have names of words already reserved for other purposes in Python:
  • What happens if?
True = 123
"Hello" = world
1stcar = 2000

Assignments

When Python sees the operator = it does the following:

  1. Evaluates the right-hand side (rhs)

    • The right of the assignment operator can be:

      • Objects: age = 21

      • Variables: my_cost = your_cost

      • Expressions: x = (x + 1) * y

  2. Assigns the resulting object to the variable on the left-hand side (lhs)

    • Only a single variable is allowed on the left side!

    • For example, x + 1 = 2 is WRONG SYNTAX!

Compound assignment operators

  • Python and other languages make available a shortcut for performing operations in variables and updating them.
  • For example,
w = 5
w += 1
print(w)

is the same as:

w = 5
w = w + 1
print(w)

You can use compound assignment with all operators!

y += 1 # add then assign value
y -= 1 # subtract then assign value
y *= 2 # multiply then assign value
y /= 3 # divide then assign value
y // = 5 # floor divide then assign value
y **= 2 # increase to the power of then assign value
y %= 3 # return remainder then assign value

Example: what will this expression do?

x *= y - 2

Converting basic objects

1. Converting to Integer

  • From Float: int(3.14) results in 3
  • From String: int("42") results in 42
  • Invalid Conversion: int("hello") raises a ValueError

2. Converting to Float

  • From Integer: float(7) results in 7.0
  • From String: float("3.14") results in 3.14
  • Invalid Conversion: float("abc") raises a ValueError

3. Converting to String

  • From Integer: str(123) results in "123"
  • From Float: str(9.99) results in "9.99"

Example Code

# Convert float to integer
num = int(5.7)  # 5

# Convert integer to string
text = str(123)  # "123"

# Convert string to float
pi = float("3.14159")  # 3.14159